import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controler {
	public KlientGui klnGui;
	public Klient kln;
	
	public Controler(KlientGui klnGui ,Klient kln){
		this.klnGui=klnGui;
		this.kln=kln;

		this.klnGui.addStartListener(new StartListener());
		this.klnGui.addCloseListener(new CloseListener());
	}
	
	class StartListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				//start watku klienta
				kln.start();
				klnGui.disableStart();
			} catch (Exception e1) {
				e1.printStackTrace();
				klnGui.showError(e1.getMessage());
			}
		}
	}
	
	class CloseListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			kln.closeThread();
			try {
				kln.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if( !kln.isAlive() ){
				klnGui.dispose();
			}
		}
	}
}
